<?php
namespace app\http\middleware;
use think\Db;
use think\facade\Config;
use traits\controller\Jump;

/**
 *  api处理
 * Class ApiMiddeware
 */
class ApiMiddeware
{
    use Jump;

    public function handle($request, \Closure $next)
    {
        Config::set("default_return_type",'json');
        //$this->apiInit($request);
        $action = str_replace("\\","/",get_module().DIRECTORY_SEPARATOR.get_controller().DIRECTORY_SEPARATOR.get_action());
        //统计调用次数 5，秒更新一次
        Db::name("oepnservice_api")->where("name",$action)->setInc("view",1,5);
        return $next($request);
    }

    /**
     * @Notes:获取访问凭证
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $request
     * @return array
     */
    protected function getToken($request)
    {
        $token  = $request->param("token");

        $secret = $request->param("secret");

        if( !$token ) $this->error("缺少访问凭证：token");

        if( !$secret) $this->error("缺少访问密匙：secret");

        //验证token 是否正确
        $data = Db::name('admin')->where("access_token",$token)->find();

        if( !$data ) $this->error("访问凭证，验证失败");

        //检测禁用状态
        if($data['status']!=1) $this->error("该账号已经被禁用");

        //验证是否开发者
        $user = Db::name("openservice_api_user")->where("uid",$data['id'])->find();
        if(!$user || $user['status']!=1) $this->error("请成为开发者后调用");

        if($data['access_secret'] != $secret ) $this->error("访问密匙secret，验证失败");

        return $data;
    }

    /**
     * @Notes:获取应用访问及api访问权限
     * @User: chenping
     * @email: 375129701@qq.com
     * @param $request
     */
    protected function getAppKey($request)
    {
        $token = $this->getToken($request);

        $appKey = $request->param("app_key");

        if( !$appKey ) $this->error("缺少应用key：app_key");

        //获取开放应用程序
        $app = Db::name("cp_openservice_apps")
            ->where("status",1)
            ->where("app_key",$appKey)
            ->find();

        //验证开放程序
        if( !$app ) $this->error("未找到该应用程序");

        $accessApp = Db::name("cp_openservice_user_app")
            ->where("uid",$token['id'])
            ->where("group_id",$app['id'])
            ->find();

        //验证应用程序访问权限
        if( !$accessApp )  $this->error("请申请该应用后使用");

        //验证接口权限情况
        $httpUrl = strtolower(str_replace("\\","/",get_module().DIRECTORY_SEPARATOR.get_controller().DIRECTORY_SEPARATOR.get_action()));

        $apiNode = Db::name('cp_oepnservice_api')
            ->where("name",$httpUrl)
            ->where("status",1)
            ->find();

        if( !$apiNode ) $this->error("接口异常请稍后再试");

        if($app['rules'] and $apiNode)
        {
            $rules = explode(",",$app['rules']);
            if(!in_array($apiNode['id'],$rules)) $this->error("未获得接口权限");
        }
    }

    /**
     * @Notes:初始化api
     * @User: chenping
     * @email: 375129701@qq.com
     */
    protected function apiInit($request)
    {
        $this->getToken($request);
        //$this->getAppKey($request);
    }
}
